﻿Imports Tag_Value_Parser.ValidationMode

Module Module1

	Sub Main()
		For Each file In IO.Directory.GetFiles(My.Application.Info.DirectoryPath, "*.log")

		Next
	End Sub



End Module

<Flags()> Public Enum ValidationMode As UInteger
	Strict = 0
	IgnoreEmptyTags = &H1
	IgnoreTagOrder = &H2
	'Unused = &H4
	'Unused = &H8
	'Unused = &H10
	'Unused = &H11
	'Unused = &H12
	'Unused = &H14
	'Unused = &H18
	'Unused = &H20

	Lienient = UInteger.MaxValue
End Enum

Class RawFixMessage
	'Question: Should this handle message length and checksum fields? There are good arguments both ways.

	Public Const Soh As Char = Chr(&H1)
	Private m_TagList As New List(Of TagValuePair)
	Private m_PrecalculatedChecksum As Integer?

	Public ReadOnly Property PrecalculatedChecksum() As Integer?
		Get
			Return m_PrecalculatedChecksum
		End Get
	End Property

	Public ReadOnly Property Checksum() As Integer
		Get
			'TODO - implement this
		End Get
	End Property

	Public Sub New(ByVal tagValueString As String, ByVal validationMode As ValidationMode)

		'Build the field list
		'TODO - This whole thing may need to be rewritten to handle binary data
		Dim fields = tagValueString.Split(Soh)
		For i As Integer = 0 To fields.Length - 1 Step 1
			If Not fields(i).Contains("=") Then Throw New FormatException("Missing '=' in section " & i)

			Dim parts = fields(i).Split(New Char() {"="c}, 1)
			Dim fieldNumber As Integer
			If Not Integer.TryParse(parts(0), fieldNumber) Then Throw New FormatException("Tag is not a number in section " & i)
			m_TagList.Add(New TagValuePair(fieldNumber, parts(1)))
		Next

		'verify that key fields are in the right places
		'TODO: First required field
		'TODO: Second required field
		'TODO: Third required field

		Dim checkSumField = m_TagList.Last
		If checkSumField.FieldNumber <> 10 Then
			Throw New FormatException("The last field isn't the Checksum(10)")
		End If
		Dim checkSum As Integer
		If Not Integer.TryParse(checkSumField.Value, checkSum) Then Throw New FormatException("Checksum (10) value isn't an integer")
		If checkSum < 0 Or checkSum >= 256 Then
			Throw New FormatException("Checksum (10) is out of range")
		End If



	End Sub

	'Public Function ToTagValueString() As String
	'	Dim result As New Text.StringBuilder
	'	For Each item In m_TagList
	'		result.Append(item.FieldNumber & "=" & item.Value & Soh)
	'	Next
	'	result.Append("10=" & Checksum.ToString("000") & Soh)
	'	Return result.ToString
	'End Function

End Class

Class TagValuePair
	'The Field Name stuff may be needed for FIX XML messages. Or maybe we will handle them in a completely different fashion

	Private m_FieldNumber As Integer
	'Private m_FieldName As String
	Private m_Value As String

	Public ReadOnly Property Value() As String
		Get
			Return m_Value
		End Get
	End Property
	'Public ReadOnly Property FieldName() As String
	'	Get
	'		Return m_FieldName
	'	End Get
	'End Property
	Public ReadOnly Property FieldNumber() As Integer
		Get
			Return m_FieldNumber
		End Get
	End Property

	Public Sub New(ByVal fieldNumber As Integer, ByVal value As String)
		If fieldNumber <= 0 Then Throw New ArgumentOutOfRangeException("fieldNumber")
		If value Is Nothing Then Throw New ArgumentNullException("value")
		m_FieldNumber = fieldNumber
		value = value
	End Sub

	'Public Sub New(ByVal fieldNumber As Integer, ByVal fieldName As String, ByVal value As String)
	'	If fieldName IsNot Nothing AndAlso fieldName.Length = 0 Then Throw New ArgumentException("The fieldName must be null or a non-empty string.", "fieldName")
	'	If fieldNumber <= 0 Then Throw New ArgumentOutOfRangeException("fieldNumber")
	'	If value Is Nothing Then Throw New ArgumentNullException("value")
	'	m_FieldNumber = fieldNumber
	'	value = value
	'End Sub

End Class

